<?php

	class Login_controller extends CI_Controller{

        var $session_id_ttl;
        
		function __construct(){
            parent::__construct();
            $this->load->library('session');
            $this->load->helper('url');
            $this->load->helper('utils');
            Parameter::initParam();
        }

		function index(){
            
            $section = "login";
            $title = _LB_LOGIN;
            $this->data['title'] = $title;
            $cfer = new Cfer(array(
            $title => base_url('login')));
            
            $return_link = '';//$this->input->get_post('return_link');
            $login = new User();

            if (!isset($_POST["login"])) {
                $_SERVER['REQUEST_URI'] = urldecode($_SERVER['REQUEST_URI']);
                $pos = strpos($_SERVER['REQUEST_URI'],'=');
                $return_link = substr($_SERVER['REQUEST_URI'],$pos+1);
                $this->data['return_link'] = $return_link;
                
                $this->load->view('login.php', $this->data);
            }
            else {
                // check if user login is deactivated or not
                if (isset($_POST['login']) && $login->isDeactivated($_POST['login']))
                {
                    $errors[] = _LB_ACCOUNT_DEACTIVATED;
                    $this->data["message_error"] = $errors;
                    $this->data["login"] = $_POST['login'];
                    $this->load->view('login.php', $this->data);
                } else {
                    //check user login
                    $acces = null;

                    if (isset($_POST["login"]) && isset($_POST["pwd"]))
                    {
                        // free all SESSION variables
                        base_url('login');

                        // check and use SHA1 to encode password
                        $acces = connecter_utilisateur($_POST["login"], $_POST["pwd"]);
                    }

                    if ($acces <> NULL && $acces->isAllowedAccessProject())
                    {

                        $acces->updateUser();

                        $this->session->set_userdata("user_id", $acces->id);
                        $this->session->set_userdata("user_name", $acces->first_name.' '.$acces->last_name);
                        $this->session->set_userdata("allow_access", $acces->allow_access);
                        $this->session->set_userdata("id_group", $acces->id_user_group);
                        if ($acces->id_user_group != ADMIN && $acces->id_user_group != EDITOR)
                        {
                            if ( isset($acces) && (!$acces->isAllowedAccessProject()) )
                            {
                                $errors[] = _LB_NOT_GRANTED;
                            }
                            else
                            {
                                $login->increaseLoginAttempts($_POST['login']);
                                if ($login->isDeactivated($_POST['login']))
                                {
                                    $errors[] = _LB_ACCOUNT_DEACTIVATED;
                                }
                                else
                                {
                                    $errors[] = _LB_INVALID_LOGIN_OR_PASSWORD;
                                }
                            }

                            $this->data["message_error"] = $errors;
                            $this->data["login"] = $_POST["login"];

                            $this->load->view('login.php', $this->data);
                            exit();
                        }

                        //serialize current user

                        $this->session->set_userdata('user_serialize', serialize($acces));

                        $login->resetLoginAttempts($acces->email);
                        if( $return_link == '')
                        {
                            redirect( base_url('user'));
                        }
                        else
                        {
                            redirect(HTTP.'://' . $_SERVER['HTTP_HOST'].$return_link);
                        }
                    }
                    else
                    {
                        if (isset($_POST["login"]))
                        {
                             if ( isset($acces) && (!$acces->isAllowedAccessProject()) )
                            {
                                $errors[] = _LB_NOT_GRANTED;
                            }
                            else
                            {
                                $login->increaseLoginAttempts($_POST['login']);
                                if ($login->isDeactivated($_POST['login']))
                                {
                                    $errors[] = _LB_ACCOUNT_DEACTIVATED;
                                }
                                else
                                {
                                    $errors[] = _LB_INVALID_LOGIN_OR_PASSWORD;
                                }
                            }

                            $this->data["message_error"] = $errors;
                            $this->data["login"] = $_POST["login"];
                        }                       
                        $this->load->view('login.php', $this->data);
                    }

                }
            }
            
            
            
            //$this->load->view('login.php', $this->data);
		}

		// You can place some more methods code here

        function logout(){
            $section = "logout";
            $this->session->sess_destroy();
            redirect_entete("");
        }
	}
